package com.may.rabbitmq.three;

import com.may.rabbitmq.utils.RabbitMqUtils;
import com.may.rabbitmq.utils.SleepUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 *
 * @author nakano_may丶
 * @date 2023/4/9
 * @Version 1.0
 * @description
 */
public class Worker02 {

    /**
     * 队列名称
     */
    public static final String TASK_QUEUE_NAME = "ack_queue";

    /**
     * 接收消息
     * @param args
     * @throws IOException
     * @throws TimeoutException
     */
    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1等待接收消息处理时间较短");

        DeliverCallback deliverCallback = ((consumerTag, message) -> {
            // 沉睡1秒
            SleepUtils.sleep(1);
            System.out.println("接收到的消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
            // 手动应答
            /**
             * 1.消息的标记 tag
             * 2.是否批量应答 false：不批量应答信道中的消息，true：批量应答
             */
            channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
        });

        // 设置不公平分发
        //int prefetchCount = 1;
        int prefetchCount = 2;
        channel.basicQos(prefetchCount);

        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, (consumerTag -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        }));
    }
}
